package com.sctvcloud.renhe.ui.adapter.holder;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.tools.DateUtils;
import com.luck.picture.lib.tools.StringUtils;
import com.ruihang.generalibrary.ui.adapter.holder.BaseRecylerHolder;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.sctvcloud.renhe.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/7/20 0020.
 */
public class RebllionCommitHolder extends BaseRecylerHolder<LocalMedia> implements View.OnClickListener, View.OnLongClickListener {
    @BindView(R.id.item_pic_pic)
    public ImageView img;
    @BindView(R.id.item_pic_del_iv)
    public ImageView del_iv;
    @BindView(R.id.item_pic_duration_cv)
    public CustomFontTextView duration_cv;


    private OnItemInternalClick onItemInternalClick;

    public OnItemInternalClick getOnItemInternalClick() {
        return onItemInternalClick;
    }

    public RebllionCommitHolder setOnItemInternalClick(OnItemInternalClick onItemInternalClick) {
        this.onItemInternalClick = onItemInternalClick;
        return this;
    }

    public RebllionCommitHolder(Context context, View itemView) {
        super(context, itemView);
        itemView.setOnClickListener(this);
        itemView.setOnLongClickListener(this);
    }

    @Override
    protected void initInject() {
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setData(LocalMedia media) {
        if (!TextUtils.isEmpty(media.getPath())) {
            Glide.with(context)
                    .load(media.getPath())
                    .into(img);
            int pictureType = PictureMimeType.isPictureType(media.getPictureType());
            int mimeType = media.getMimeType();
            long duration = media.getDuration();
            duration_cv.setVisibility(pictureType == PictureConfig.TYPE_VIDEO
                    ? View.VISIBLE : View.GONE);
            if (mimeType == PictureMimeType.ofAudio()) {
                // duration_cv.setVisibility(View.VISIBLE);
                Drawable drawable = ContextCompat.getDrawable(context, R.drawable.picture_audio);
                StringUtils.modifyTextViewDrawable(duration_cv, drawable, 0);
                duration_cv.setText(DateUtils.timeParse(duration));
            } else {
                Drawable drawable = ContextCompat.getDrawable(context, R.drawable.video_icon);
                StringUtils.modifyTextViewDrawable(duration_cv, drawable, 0);
                duration_cv.setText(DateUtils.timeParse(duration));
            }
        }
    }

    @Override
    public void setData(LocalMedia localMedia, boolean isEditing) {
        super.setData(localMedia, isEditing);
        if (!TextUtils.isEmpty(localMedia.getPath())) {
            del_iv.setVisibility(isEditing ? View.VISIBLE : View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        if (internalClick != null) {
            internalClick.onItemInternalClick(itemView, v, getAdapterPosition());
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (onItemInternalClick != null) {
            onItemInternalClick.onItemInternalClick(itemView, v, getAdapterPosition());
        }
        return false;
    }

}
